﻿/**
 * @file setting_wlan.c
 * set wifi from client app other than setting ui
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WLAN != 0

#if WLAN_MANUAL_SET_WIFI_SUPP == 0

/*********************
 *      DEFINES
 *********************/
#define WLAN_CLIENT_TEST_MODE       0 // simulate client app which wifi is set by

#define WLAN_DEF_FONT               (LV_THEME_WATCH_NIGHT_FONT_NORMAL)

#define WLAN_STYLE_FONT             (&lv_watch_font20)
#define WLAN_STYLE_FONT_GRAY        (&lv_watch_font20_gray)
#define WLAN_STYLE_FONT_BLACK       (&lv_watch_font20_black)
#define WLAN_STYLE_FONT_SMALL       (&lv_watch_font20)
#define WLAN_STYLE_FONT_SMALL_BLACK (&lv_watch_font20_black)
#define WLAN_STYLE_FONT_SMALL_GRAY  (&lv_watch_font20_gray)

/**********************
 *      TYPEDEFS
 **********************/
typedef struct {
    lv_watch_obj_ext_t oldext;
    lv_obj_t * note_label;
    lv_obj_t * list;
} wlan_ext_t;

typedef struct {
    lv_watch_obj_ext_t oldext;
    lv_obj_t * list;
} wlan_client_saved_nw_ext_t;

typedef struct {
    lv_watch_obj_ext_t oldext;
    lv_obj_t * ssid_label;
    lv_obj_t * pwd_label;
    lv_obj_t * ok_btn;
    lv_obj_t * cancel_btn;
    char * ssid;
    char * pwd;
} wlan_client_add_nw_ext_t;

typedef bool (*WLAN_DIALOG_OK_BTN_EVENT_CB)(lv_obj_t * btn);

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void wlan_event_cb(HAL_WLAN_EVENT_T event);
static void * wlan_get_ext(lv_watch_Activity_Id_t actId);
static void wlan_add_note_label(lv_obj_t ** note_label, lv_obj_t * par, uint16_t txt_id);
static lv_obj_t * wlan_create(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void wlan_init(void)
{
    hal_wlan_init(false, watch_get_lcd_status, wlan_event_cb);
}

void wlan_create_btn_action(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;

    if(LV_EVENT_CLICKED != e) return;

    lv_watch_png_cache_all_free();
    lv_obj_t * obj = wlan_create(NULL);
    LV_ASSERT_MEM(obj);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void * wlan_get_ext(lv_watch_Activity_Id_t actId)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(actId);
    if(NULL == activity_obj) return NULL;

    lv_obj_t * obj = NULL;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &obj);
    if(NULL == obj) return NULL;

    return lv_obj_get_ext_attr(obj);
}

static lv_obj_t * wlan_add_title(lv_obj_t * par, uint32_t txt_id)
{
    lv_obj_t * cont = lv_cont_create(par, NULL);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(cont, false);
    lv_cont_set_fit(cont, LV_FIT_TIGHT);

    lv_obj_t * title_label = lv_label_create(cont, NULL);
    lv_obj_add_style(title_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_SMALL_GRAY);
    lv_label_set_align(title_label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_long_mode(title_label, LV_LABEL_LONG_SROLL_CIRC);
    lv_obj_set_width(title_label, lv_obj_get_width_fit(par));
    lv_label_set_text_id(title_label, txt_id);

    lv_obj_align(cont, par, LV_ALIGN_IN_TOP_MID, 0,
                 lv_obj_get_style_pad_top(par, LV_CONT_PART_MAIN));

    return cont;
}

static lv_obj_t * wlan_add_bg(lv_obj_t * par)
{
    lv_obj_t * cont = lv_cont_create(par, NULL);
    lv_obj_set_style_local_bg_color(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_pad_top(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_bottom(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_size(cont, lv_obj_get_width(par), lv_obj_get_height(par));
    lv_obj_align(cont, par, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_click(cont, false);
    return cont;
}

static void wlan_list_set_common_param(lv_obj_t * list)
{
    lv_obj_add_style(list, LV_LIST_PART_BG, &lv_style_transp_tight);
    lv_obj_add_style(list, LV_LIST_PART_SCROLLABLE, &lv_style_transp_tight);
    lv_obj_set_style_local_pad_inner(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 4);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_SCROLLBAR, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(list, LV_LIST_PART_SCROLLBAR, LV_STATE_DEFAULT, -4);
    lv_obj_set_style_local_pad_top(list, LV_LIST_PART_SCROLLBAR, LV_STATE_DEFAULT, 1);
    lv_obj_set_style_local_pad_bottom(list, LV_LIST_PART_SCROLLBAR, LV_STATE_DEFAULT, 1);
    lv_obj_set_style_local_bg_color(list, LV_LIST_PART_SCROLLBAR, LV_STATE_DEFAULT,
                                    LV_COLOR_GRAY);

    lv_list_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    lv_page_set_scrl_layout(list, LV_LAYOUT_COLUMN_MID);

    lv_watch_obj_add_element(list);
    lv_watch_obj_add_element(lv_page_get_scrollable(list));
}

static void wlan_add_saved_nw_list(wlan_ext_t * ext)
{
    if(NULL == ext) {
        printf("%s: not in wlan ui\n", __FUNCTION__);
        return;
    }

    lv_page_set_scrollable_fit2(ext->list, LV_FIT_NONE, LV_FIT_NONE);

    lv_list_clean(ext->list);

    hal_wlan_info_t * info = hal_wlan_get_info();
    if((NULL == info) || (HAL_WLAN_STATE_INIT == info->state)) {
        wlan_add_note_label(&ext->note_label, lv_obj_get_parent(ext->list),
                            WATCH_TEXT_ID_NOT_SET_WIFI);
    } else {
        if(ext->note_label) {
            lv_obj_del(ext->note_label);
            ext->note_label = NULL;
        }

        lv_bidi_dir_t dir = lv_obj_get_base_dir(ext->list);

        bool is_act = false;
        bool is_opening = false;
        hal_wlan_nw_t * oper_nw = NULL;
        if((HAL_WLAN_AP_STATE_ACT == info->proc.proc_state)
           || (HAL_WLAN_AP_STATE_NO_INTERNET == info->proc.proc_state)) {
            is_act = true;
            oper_nw = info->proc.ap->nw;
            printf("%s: act ap, ssid %s\n", __FUNCTION__, info->proc.ap->nw->ssid);
        } else if(info->opening_nw) {
            is_opening = true;
            oper_nw = info->opening_nw;
            printf("%s: opening nw, ssid %s\n", __FUNCTION__, info->opening_nw->ssid);
        } else if(HAL_WLAN_AP_STATE_OPENING == info->proc.proc_state) {
            if(info->proc.ap->is_hidden
               || (HAL_WLAN_AP_STATE_RETRY == info->proc.ap->state)) {
                printf("%s: not display hidden or retrying ap\n", __FUNCTION__);
            } else {
                is_opening = true;
                oper_nw = info->proc.ap->nw;
                printf("%s: opening ap, ssid %s\n", __FUNCTION__, info->proc.ap->nw->ssid);
            }
        } else if(HAL_WLAN_AP_STATE_GETTING_IP == info->proc.proc_state) {
            is_opening = true;
            oper_nw = info->proc.ap->nw;
            printf("%s: getting ip, ssid %s\n", __FUNCTION__, info->proc.ap->nw->ssid);
        }

        if(is_act || is_opening) {
            lv_obj_t * cont = lv_cont_create(ext->list, NULL);
            lv_cont_set_fit2(cont, LV_FIT_NONE, LV_FIT_TIGHT);
            if(LV_BIDI_DIR_RTL == dir) {
                lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_RIGHT);
            } else {
                lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_LEFT);
            }
            lv_obj_set_width(cont, lv_obj_get_width(ext->list) - 20);
            lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_transp_tight);
            lv_page_glue_obj(cont, true);
            lv_obj_set_click(cont, false);

            lv_obj_t * label = lv_label_create(cont, NULL);
            lv_obj_add_style(label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
            lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
            lv_obj_set_width(label, lv_obj_get_width(cont));
            lv_label_set_text(label, oper_nw->ssid);

            label = lv_label_create(cont, NULL);
            lv_obj_add_style(label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_SMALL_GRAY);
            if(is_act) {
                lv_label_set_text_id(label, WATCH_TEXT_ID_CONNECTED);
            } else {
                lv_label_set_text_id(label, WATCH_TEXT_ID_CONNECTION);
            }
        }

        lv_ll_t * nw_list = hal_wlan_get_nw_list();
        hal_wlan_nw_t * nw = _lv_ll_get_head(nw_list);
        while(nw) {
            if(oper_nw != nw) {
                lv_obj_t * cont = lv_cont_create(ext->list, NULL);
                lv_cont_set_fit2(cont, LV_FIT_NONE, LV_FIT_TIGHT);
                if(LV_BIDI_DIR_RTL == dir) {
                    lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_RIGHT);
                } else {
                    lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_LEFT);
                }
                lv_obj_set_width(cont, lv_obj_get_width(ext->list) - 20);
                lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_transp_tight);
                lv_page_glue_obj(cont, true);
                lv_obj_set_click(cont, false);

                lv_obj_t * label = lv_label_create(cont, NULL);
                lv_obj_add_style(label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
                lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
                lv_obj_set_width(label, lv_obj_get_width(cont));
                lv_label_set_text(label, nw->ssid);

                label = lv_label_create(cont, NULL);
                lv_obj_add_style(label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_SMALL_GRAY);
                lv_label_set_text_id(label, WATCH_TEXT_ID_NO_CONNECTION);
            }
            nw = _lv_ll_get_next(nw_list, nw);
        }
    }

    lv_page_set_scrollable_fit2(ext->list, LV_FIT_NONE, LV_FIT_TIGHT);
}

#if WLAN_CLIENT_TEST_MODE != 0
static void wlan_del_ui(lv_watch_Activity_Id_t act_id)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(act_id);
    if(NULL == activity_obj) return;

    lv_watch_activity_ext_t * ext = lv_obj_get_ext_attr(activity_obj);
    LV_ASSERT_MEM(ext);
    if(ext->prepare_destory) {
        ext->prepare_destory(activity_obj);
    }
    lv_obj_del(activity_obj);
}

static void wlan_btn_set_normal_style(lv_obj_t * btn)
{
    lv_obj_clean_style_list(btn, LV_BTN_PART_MAIN);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_style_pretty);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_40);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_OPA_COVER);
}

static void wlan_btn_set_transp_style(lv_obj_t * btn)
{
    lv_obj_clean_style_list(btn, LV_BTN_PART_MAIN);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 4);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 4);
    lv_obj_set_style_local_pad_left(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_inner(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 4);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 4);
    lv_obj_set_style_local_pad_left(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 5);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 5);
    lv_obj_set_style_local_pad_inner(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 2);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_grad_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED,
                                         LV_COLOR_MAKE(0x10, 0x26, 0x3c));
    lv_obj_set_style_local_bg_grad_dir(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_GRAD_DIR_VER);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_OPA_20);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 10);
}

static lv_obj_t * wlan_dialog_get_ok_btn(lv_obj_t * content)
{
    lv_obj_t * cont = lv_obj_get_parent(content);
    return lv_obj_get_child_back(lv_obj_get_child_back(cont, content), NULL);
}

static void wlan_dialog_anim_end_cb(lv_anim_t * anim)
{
    lv_obj_t * cont = (lv_obj_t *)anim->var;

    lv_obj_t * bg = lv_obj_get_parent(cont);
    bool is_del_dialog_only = lv_watch_get_free_num(bg);
    if(is_del_dialog_only) {
        lv_obj_del(bg);
    } else {
        lv_watch_Activity_Id_t act_id = lv_watch_get_free_num(cont);
        wlan_del_ui(act_id);
    }
}

static void wlan_dialog_anim(lv_obj_t * obj, lv_coord_t start, lv_coord_t end)
{
    lv_anim_path_t path;
    lv_anim_path_init(&path);
    lv_anim_path_set_cb(&path, lv_anim_path_linear);
    lv_anim_t a = {};
    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_values(&a, start, end);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
    if(start < end) {
        lv_anim_set_ready_cb(&a, wlan_dialog_anim_end_cb);
    }
    lv_anim_set_time(&a, 100);
    lv_anim_set_path(&a, &path);
    lv_anim_start(&a);
}

static void wlan_dialog_bg_event_cb(lv_obj_t * cont, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_obj_t * child = lv_obj_get_child(cont, NULL);
    if(lv_anim_get(child, NULL)) {
        printf("%s: anim\n", __FUNCTION__);
        return;
    }

    printf("%s:\n", __FUNCTION__);

    wlan_dialog_anim(child, lv_obj_get_y(child), lv_obj_get_height(cont));
}

static void wlan_dialog_ok_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    bool ret = true;

    WLAN_DIALOG_OK_BTN_EVENT_CB cb = (WLAN_DIALOG_OK_BTN_EVENT_CB)lv_watch_get_free_ptr(btn);
    if(cb) {
        ret = cb(btn);
    }
    printf("%s: ret %d\n", __FUNCTION__, ret);
    if(ret) {
        lv_obj_t * cont = lv_obj_get_parent(lv_obj_get_parent(btn));
        lv_anim_del(cont, NULL);
        lv_watch_set_free_num(lv_obj_get_parent(cont), false);
        wlan_dialog_anim(cont, lv_obj_get_y(cont), lv_obj_get_height(lv_obj_get_parent(cont)));
        lv_obj_set_click(btn, false);
        lv_obj_set_click(lv_obj_get_child_back(cont, btn), false);
    }
}

static void wlan_dialog_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    printf("%s\n", __FUNCTION__);

    lv_obj_t * cont = lv_obj_get_parent(lv_obj_get_parent(btn));
    lv_anim_del(cont, NULL);
    wlan_dialog_anim(cont, lv_obj_get_y(cont), lv_obj_get_height(lv_obj_get_parent(cont)));
    lv_obj_set_click(btn, false);
    lv_obj_set_click(lv_obj_get_child(cont, btn), false);
}

static lv_obj_t * wlan_dialog_create(lv_obj_t * obj, uint16_t act_id, bool is_del_dialog_only,
                                     WLAN_DIALOG_OK_BTN_EVENT_CB ok_cb)
{
    if(false == is_del_dialog_only) {
        lv_obj_t * img = lv_obj_get_parent(obj);
        lv_obj_set_style_local_image_opa(img, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_0);
        lv_obj_t * activity_obj = lv_obj_get_parent(img);
        lv_obj_set_style_local_bg_opa(activity_obj, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_0);
    }

    lv_coord_t w = lv_obj_get_width(obj);
    lv_coord_t h = lv_obj_get_height(obj);

    lv_obj_t * bg = lv_cont_create(obj, NULL);
    lv_obj_add_style(bg, LV_CONT_PART_MAIN, &lv_style_transp_tight);
    lv_cont_set_layout(bg, LV_LAYOUT_OFF);
    lv_obj_set_size(bg, w, h);
    lv_watch_set_free_num(bg, is_del_dialog_only); // delete act id if is_del_dialog_only is false
    lv_obj_set_event_cb(bg, wlan_dialog_bg_event_cb);
    lv_obj_align(bg, obj, LV_ALIGN_CENTER, 0, 0);

    lv_coord_t font_h = lv_font_get_line_height(WLAN_DEF_FONT);

    lv_obj_t * cont = lv_cont_create(bg, NULL);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_obj_set_style_local_bg_color(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT,
                                    LV_COLOR_MAKE(60, 60, 60));
    lv_coord_t radius = w / 10;
    lv_obj_set_style_local_radius(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, radius);
    lv_coord_t ver = font_h / 2;
    lv_obj_set_style_local_pad_top(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, ver);
    lv_obj_set_style_local_pad_bottom(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, ver + radius);
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, font_h / 3);
    lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_MID);
    lv_cont_set_fit2(cont, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(cont, w);
    lv_watch_set_free_num(cont, act_id);

    lv_obj_t * title_label = lv_label_create(cont, NULL);
    lv_obj_add_style(title_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
    lv_label_set_align(title_label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_long_mode(title_label, LV_LABEL_LONG_SROLL_CIRC);
    lv_obj_set_width(title_label, w - 20);

    lv_obj_t * content = lv_cont_create(cont, NULL);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_style_transp_tight);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    lv_cont_set_fit2(content, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(content, w);
    lv_obj_set_click(content, false);

    lv_obj_t * btn_cont = lv_cont_create(cont, content);

    lv_obj_t * ok_btn = lv_btn_create(btn_cont, NULL);
    wlan_btn_set_normal_style(ok_btn);
    lv_obj_set_size(ok_btn, w / 3, font_h * 3 / 2);
    lv_obj_set_event_cb(ok_btn, wlan_dialog_ok_btn_event_cb);
    lv_watch_set_free_ptr(ok_btn, ok_cb);

    lv_obj_t * ok_label = lv_label_create(ok_btn, NULL);
    lv_obj_set_style_local_text_font(ok_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, WLAN_DEF_FONT);

    lv_obj_t * cancel_btn = lv_btn_create(btn_cont, ok_btn);
    lv_obj_set_event_cb(cancel_btn, wlan_dialog_cancel_btn_event_cb);

    lv_obj_t * cancel_label = lv_label_create(cancel_btn, ok_label);
    lv_obj_set_style_local_text_color(cancel_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT,
                                      LV_COLOR_WHITE);
    lv_label_set_text_id(cancel_label, WATCH_TEXT_ID_CANCEL);
    lv_obj_align(cancel_label, cancel_btn, LV_ALIGN_CENTER, 0, 0);

    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(cont)) {
        lv_obj_align(cancel_btn, btn_cont, LV_ALIGN_IN_TOP_RIGHT, -w / 9, 0);
        lv_obj_align(ok_btn, btn_cont, LV_ALIGN_IN_TOP_LEFT, w / 9, 0);
    } else {
        lv_obj_align(ok_btn, btn_cont, LV_ALIGN_IN_TOP_RIGHT, -w / 9, 0);
        lv_obj_align(cancel_btn, btn_cont, LV_ALIGN_IN_TOP_LEFT, w / 9, 0);
    }

    return content;
}

static void wlan_dialog_anim_start(lv_obj_t * cont)
{
    lv_obj_t * bg = lv_obj_get_parent(cont);
    lv_coord_t cont_h = lv_obj_get_height(cont);
    lv_coord_t h = lv_obj_get_height(bg);
    wlan_dialog_anim(cont, h, h - cont_h + lv_obj_get_style_radius(cont, LV_CONT_PART_MAIN));
}

#if USE_LV_WATCH_KEYPAD != 0
static void wlan_btn_set_input_style(lv_obj_t * btn)
{
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_style_pretty);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_grad_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_left(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
}

static void wlan_client_add_nw_ok_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    wlan_client_add_nw_ext_t * ext = wlan_get_ext(ACT_ID_WLAN_ADD_NW);
    if(NULL == ext) return;

    if(NULL == ext->ssid) {
        return;
    }

    printf("%s: ssid %s, pwd %s\n", __FUNCTION__, ext->ssid, ext->pwd);

    hal_wlan_wifi_info_t info = {};
    info.cmd = 1;
    Hal_Mem_Copy(info.ssid, ext->ssid, strlen(ext->ssid));
    if(ext->pwd) {
        Hal_Mem_Copy(info.pwd, ext->pwd, strlen(ext->pwd));
    }
    hal_wlan_set_wifi_ind(&info);
    wlan_del_ui(ACT_ID_WLAN_ADD_NW);
}

static void wlan_client_add_nw_get_ssid(char * text)
{
    wlan_client_add_nw_ext_t * ext = wlan_get_ext(ACT_ID_WLAN_ADD_NW);
    if(NULL == ext) return;

    uint8_t len = 0;
    if(text) {
        len = strlen(text);
    }
    if((0 == len) && ext->ssid) {
        lv_obj_add_style(ext->ssid_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_GRAY);
        lv_label_set_text_id(ext->ssid_label, WATCH_TEXT_ID_SSID);
        Hal_Mem_Free(ext->ssid);
        ext->ssid = NULL;
        lv_obj_set_style_local_text_color(lv_obj_get_child(ext->ok_btn, NULL),
                                          LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    } else if(0 < len) {
        if(NULL == ext->ssid) {
            ext->ssid = text;
            lv_obj_add_style(ext->ssid_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_BLACK);
            lv_label_set_text(ext->ssid_label, text);
            lv_obj_set_style_local_text_color(lv_obj_get_child(ext->ok_btn, NULL),
                                              LV_LABEL_PART_MAIN, LV_STATE_DEFAULT,
                                              LV_COLOR_WHITE);
        } else if(0 != strcmp(ext->ssid, text)) {
            Hal_Mem_Free(ext->ssid);
            ext->ssid = text;
            lv_label_set_text(ext->ssid_label, text);
        }
    }
}

static void wlan_client_add_nw_ssid_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    wlan_client_add_nw_ext_t * ext = wlan_get_ext(ACT_ID_WLAN_ADD_NW);
    if(NULL == ext) return;

    keypad_para_t param;
    param.text = ext->ssid;
    param.password_mode = false;
    param.max_length = HAL_WLAN_MAX_SSID_LEN;
    param.min_length = 1;
    param.func = wlan_client_add_nw_get_ssid;
    keypad_create_btn_action(&param);
}

static void wlan_client_get_pwd_handle(char * text, char * ssid, char ** pwd, lv_obj_t * pwd_label,
                                       lv_obj_t * btn)
{
    uint8_t len = 0;
    if(text) {
        len = strlen(text);
    }
    if((0 == len) && (*pwd)) {
        Hal_Mem_Free(*pwd);
        *pwd = NULL;

        lv_obj_add_style(pwd_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_GRAY);
        lv_label_set_text_id(pwd_label, WATCH_TEXT_ID_PASSWORD);
    } else if(0 < len) {
        if(NULL == *pwd) {
            lv_obj_add_style(pwd_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_BLACK);
        } else if(0 == strcmp(*pwd, text)) {
            Hal_Mem_Free(text);
            return;
        } else {
            Hal_Mem_Free(*pwd);
        }
        *pwd = text;

        char temp_pwd[64 + 1] = {};
        for(uint8_t i = 0;i < len;i++) {
            temp_pwd[i] = '*';
        }
        lv_label_set_text(pwd_label, temp_pwd);
    }
}

static void wlan_client_add_nw_get_pwd(char * text)
{
    wlan_client_add_nw_ext_t * ext = wlan_get_ext(ACT_ID_WLAN_ADD_NW);
    if(NULL == ext) return;

    wlan_client_get_pwd_handle(text, ext->ssid, &ext->pwd, ext->pwd_label, ext->ok_btn);
}

static void wlan_client_add_nw_pwd_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    wlan_client_add_nw_ext_t * ext = wlan_get_ext(ACT_ID_WLAN_ADD_NW);
    if(NULL == ext) return;

    keypad_para_t param;
    param.text = ext->pwd;
    param.password_mode = true;
    param.max_length = HAL_WLAN_MAX_PWD_LEN;
    param.min_length = HAL_WLAN_MIN_WEP_PWD_LEN;
    param.func = wlan_client_add_nw_get_pwd;
    keypad_create_btn_action(&param);
}

static void wlan_client_add_nw_prepare_destroy(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);

    lv_watch_png_cache_all_free();

    wlan_client_add_nw_ext_t * ext = wlan_get_ext(ACT_ID_WLAN_ADD_NW);
    if(NULL == ext) {
        printf("%s: error, ui lost!\n", __FUNCTION__);
        return;
    }

    if(ext->ssid) {
        Hal_Mem_Free(ext->ssid);
    }
    if(ext->pwd) {
        Hal_Mem_Free(ext->pwd);
    }
}

static void wlan_client_add_nw_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    wlan_del_ui(ACT_ID_WLAN_ADD_NW);
}

static void wlan_client_add_nw_create(void)
{
    lv_watch_activity_ext_t activity_ext;
    memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
    activity_ext.actId = ACT_ID_WLAN_ADD_NW;
    activity_ext.create = NULL;
    activity_ext.prepare_destory = wlan_client_add_nw_prepare_destroy;
    lv_obj_t * activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    LV_ASSERT_MEM(activity_obj);

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    wlan_client_add_nw_ext_t * ext
        = lv_obj_allocate_ext_attr(obj, sizeof(wlan_client_add_nw_ext_t));
    LV_ASSERT_MEM(ext);

    ext->ssid = NULL;
    ext->pwd = NULL;
    ext->pwd_label = NULL;

    lv_obj_t * cont = wlan_add_bg(obj);

    lv_obj_t * title_cont = wlan_add_title(cont, WATCH_TEXT_ID_ADD_NW);

    lv_coord_t obj_w = lv_obj_get_width_fit(cont);
    lv_bidi_dir_t dir = lv_obj_get_base_dir(cont);

    lv_obj_t * ssid_btn = lv_btn_create(cont, NULL);
    wlan_btn_set_input_style(ssid_btn);
    lv_btn_set_fit2(ssid_btn, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(ssid_btn, obj_w);
    lv_obj_set_event_cb(ssid_btn, wlan_client_add_nw_ssid_btn_event_cb);
    lv_obj_align(ssid_btn, title_cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_watch_obj_add_element(ssid_btn);

    ext->ssid_label = lv_label_create(ssid_btn, NULL);
    lv_obj_add_style(ext->ssid_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT_GRAY);
    lv_label_set_long_mode(ext->ssid_label, LV_LABEL_LONG_SROLL_CIRC);
    lv_obj_set_width(ext->ssid_label, lv_obj_get_width_fit(ssid_btn));
    lv_label_set_text_id(ext->ssid_label, WATCH_TEXT_ID_SSID);

    lv_obj_t * pwd_btn = lv_btn_create(cont, ssid_btn);
    lv_obj_set_event_cb(pwd_btn, wlan_client_add_nw_pwd_btn_event_cb);
    lv_obj_align(pwd_btn, ssid_btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_watch_obj_add_element(pwd_btn);

    ext->pwd_label = lv_label_create(pwd_btn, ext->ssid_label);
    lv_label_set_text_id(ext->pwd_label, WATCH_TEXT_ID_PASSWORD);

    ext->ok_btn = lv_btn_create(cont, NULL);
    lv_obj_set_size(ext->ok_btn, lv_obj_get_width(cont) * 3 / 7, lv_obj_get_height(ssid_btn));
    wlan_btn_set_normal_style(ext->ok_btn);
    lv_obj_set_event_cb(ext->ok_btn, wlan_client_add_nw_ok_btn_event_cb);
    lv_watch_obj_add_element(ext->ok_btn);

    lv_obj_t * ok_label = lv_label_create(ext->ok_btn, NULL);
    lv_obj_add_style(ok_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
    lv_obj_set_style_local_text_color(ok_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_label_set_text_id(ok_label, WATCH_TEXT_ID_OK);
    lv_obj_align(ok_label, ext->ok_btn, LV_ALIGN_CENTER, 0, 0);

    ext->cancel_btn = lv_btn_create(cont, ext->ok_btn);
    wlan_btn_set_normal_style(ext->cancel_btn);
    lv_obj_set_event_cb(ext->cancel_btn, wlan_client_add_nw_cancel_btn_event_cb);
    lv_watch_obj_add_element(ext->cancel_btn);

    lv_obj_t * cancel_label = lv_label_create(ext->cancel_btn, NULL);
    lv_obj_add_style(cancel_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
    lv_label_set_text_id(cancel_label, WATCH_TEXT_ID_CANCEL);
    lv_obj_align(cancel_label, ext->cancel_btn, LV_ALIGN_CENTER, 0, 0);

    lv_coord_t pad_bottom = lv_obj_get_style_pad_bottom(cont, LV_CONT_PART_MAIN);
    if(LV_BIDI_DIR_RTL == dir) {
        lv_obj_align(ext->cancel_btn, cont, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -pad_bottom);
        lv_obj_align(ext->ok_btn, cont, LV_ALIGN_IN_BOTTOM_LEFT, 10, -pad_bottom);
    } else {
        lv_obj_align(ext->ok_btn, cont, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -pad_bottom);
        lv_obj_align(ext->cancel_btn, cont, LV_ALIGN_IN_BOTTOM_LEFT, 10, -pad_bottom);
    }
}

static void wlan_client_add_nw_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    wlan_client_add_nw_create();
}
#endif /* USE_LV_WATCH_KEYPAD */

static bool wlan_client_del_saved_nw_ok_btn_event_cb(lv_obj_t * btn)
{
    wlan_client_saved_nw_ext_t * ext = wlan_get_ext(ACT_ID_WLAN_SAVED_NW);
    if(NULL == ext) {
        printf("%s: warning, not find wlan saved ap\n", __FUNCTION__);
        return true;
    }

    lv_obj_t * nw_btn = lv_watch_get_free_ptr(lv_obj_get_child(btn, NULL));
    if(NULL == nw_btn) {
        printf("%s:warning, no nw_btn\n", __FUNCTION__);
        return true;
    }

    hal_wlan_nw_t * nw = lv_watch_get_free_ptr(lv_obj_get_child(nw_btn, NULL));
    if(NULL == nw) {
        printf("%s:warning, no nw\n", __FUNCTION__);
        return true;
    }

    printf("%s: ssid %s\n", __FUNCTION__, nw->ssid);

    hal_wlan_wifi_info_t info = {};
    info.cmd = 0;
    Hal_Mem_Copy(info.ssid, nw->ssid, HAL_WLAN_MAX_SSID_LEN + 1);
    hal_wlan_set_wifi_ind(&info);

    lv_obj_t * scrl = lv_obj_get_parent(nw_btn);
    lv_obj_del(nw_btn);

    if(NULL == _lv_ll_get_head(&scrl->child_ll)) {
        lv_obj_t * cont = lv_obj_get_parent(ext->list);
        lv_obj_del(ext->list);
        ext->list = NULL;

        lv_obj_t * empty_label = lv_label_create(cont, NULL);
        lv_obj_add_style(empty_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
        lv_label_set_text_id(empty_label, WATCH_TEXT_ID_EMPTY);
        lv_obj_align(empty_label, cont, LV_ALIGN_CENTER, 0, -20);
    }

    return true;
}

static void wlan_client_del_saved_nw_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_WLAN_SAVED_NW);
    if(NULL == activity_obj) {
        printf("%s: warning, activity_obj is NULL\n", __FUNCTION__);
        return;
    }

    lv_obj_t * obj = NULL;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &obj);
    if(NULL == obj) {
        printf("%s: warning, obj is NULL\n", __FUNCTION__);
        return;
    }

    hal_wlan_nw_t * nw = lv_watch_get_free_ptr(lv_obj_get_child(btn, NULL));
    if(NULL == nw) {
        printf("%s: warning, no nw\n", __FUNCTION__);
        return;
    }

    lv_obj_t * content = wlan_dialog_create(obj, ACT_ID_WLAN_SAVED_NW, true,
                                            wlan_client_del_saved_nw_ok_btn_event_cb);

    lv_obj_t * cont = lv_obj_get_parent(content);

    lv_obj_t * title_label = lv_obj_get_child_back(cont, NULL);
    lv_label_set_text(title_label, nw->ssid);

    lv_obj_t * label = lv_label_create(content, title_label);
    lv_label_set_text_id(label, WATCH_TEXT_ID_DEL_NW);

    lv_obj_t * ok_btn = wlan_dialog_get_ok_btn(content);
    wlan_btn_set_normal_style(ok_btn);
    lv_obj_set_style_local_bg_color(ok_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT,
                                    LV_COLOR_MAKE(17, 155, 238));

    lv_obj_t * ok_label = lv_obj_get_child(ok_btn, NULL);
    lv_obj_set_style_local_text_color(ok_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_label_set_text_id(ok_label, WATCH_TEXT_ID_OK);
    lv_obj_align(ok_label, ok_btn, LV_ALIGN_CENTER, 0, 0);
    lv_watch_set_free_ptr(ok_label, btn);

    wlan_dialog_anim_start(cont);
}

static void wlan_client_saved_nw_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);
}

static void wlan_client_saved_nw_create(void)
{
    lv_watch_activity_ext_t activity_ext;
    memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
    activity_ext.actId = ACT_ID_WLAN_SAVED_NW;
    activity_ext.create = NULL;
    activity_ext.prepare_destory = wlan_client_saved_nw_prepare_destory;
    lv_obj_t * activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    LV_ASSERT_MEM(activity_obj);

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    wlan_client_saved_nw_ext_t * ext
        = lv_obj_allocate_ext_attr(obj, sizeof(wlan_client_saved_nw_ext_t));
    LV_ASSERT_MEM(ext);

    wlan_ext_t * wlan_ext = wlan_get_ext(ACT_ID_WLAN);
    LV_ASSERT_MEM(wlan_ext);

    lv_obj_t * cont = wlan_add_bg(obj);

    lv_obj_t * title_cont = wlan_add_title(cont, WATCH_TEXT_ID_SAVED_NW);

    lv_ll_t * nw_list = hal_wlan_get_nw_list();
    if(NULL == nw_list) {
        printf("%s: warning, nw_list is NULL\n", __FUNCTION__);
        return;
    }

    ext->list = NULL;
    hal_wlan_nw_t * node = _lv_ll_get_head(nw_list);
    while(node) {
        if(NULL == ext->list) {
            ext->list = lv_list_create(cont, NULL);
            wlan_list_set_common_param(ext->list);
            lv_coord_t list_h = lv_obj_get_height(cont) - lv_obj_get_height(title_cont) - 12;
            lv_obj_set_size(ext->list, lv_obj_get_width_fit(cont), list_h);
            lv_obj_align(ext->list, cont, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
        }

        lv_obj_t * btn = lv_btn_create(ext->list, NULL);
        wlan_btn_set_transp_style(btn);
        lv_btn_set_fit2(btn, LV_FIT_NONE, LV_FIT_TIGHT);
        lv_obj_set_width(btn, lv_obj_get_width(ext->list));
        lv_btn_set_layout(btn, LV_LAYOUT_CENTER);
        lv_obj_set_event_cb(btn, wlan_client_del_saved_nw_btn_event_cb);
        lv_page_glue_obj(btn, true);
        lv_watch_obj_add_element(btn);

        lv_obj_t * label = lv_label_create(btn, NULL);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
        lv_obj_set_width(label, lv_obj_get_width(btn) - 10);
        lv_label_set_text(label, node->ssid);
        lv_watch_set_free_ptr(label, node);

        node = _lv_ll_get_next(nw_list, node);
    }
    if(NULL == ext->list) {
        lv_obj_t * empty_label = lv_label_create(cont, NULL);
        lv_obj_add_style(empty_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
        lv_label_set_text_id(empty_label, WATCH_TEXT_ID_EMPTY);
        lv_obj_align(empty_label, cont, LV_ALIGN_CENTER, 0, -20);
    }
}

static void wlan_client_saved_nw_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    wlan_client_saved_nw_create();
}

static void wlan_client_test_mode_prepare_destroy(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);
}

static void wlan_client_test_mode_create(void)
{
    lv_watch_activity_ext_t activity_ext;
    memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
    activity_ext.actId = ACT_ID_WLAN_OPTION;
    activity_ext.create = NULL;
    activity_ext.prepare_destory = wlan_client_test_mode_prepare_destroy;
    lv_obj_t * activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    LV_ASSERT_MEM(activity_obj);

    wlan_ext_t * ext = wlan_get_ext(ACT_ID_WLAN);
    if(NULL == ext) return;

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);

    lv_obj_t * cont = wlan_add_bg(obj);

    lv_obj_t * title_cont = wlan_add_title(cont, WATCH_TEXT_ID_TEST_MODE);

    lv_obj_t * list = lv_list_create(cont, NULL);
    wlan_list_set_common_param(list);
    lv_obj_set_size(list, lv_obj_get_width_fit(cont),
                    lv_obj_get_height(cont) - lv_obj_get_y(title_cont)
                    - lv_obj_get_height(title_cont) - 6);
    lv_obj_align(list, cont, LV_ALIGN_IN_BOTTOM_MID, 0, -2);

    lv_obj_t * saved_ap_btn = lv_btn_create(list, NULL);
    wlan_btn_set_transp_style(saved_ap_btn);
    lv_btn_set_fit2(saved_ap_btn, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(saved_ap_btn, lv_obj_get_width(list));
    lv_btn_set_layout(saved_ap_btn, LV_LAYOUT_ROW_MID);
    lv_obj_set_event_cb(saved_ap_btn, wlan_client_saved_nw_btn_event_cb);
    lv_page_glue_obj(saved_ap_btn, true);
    lv_watch_obj_add_element(saved_ap_btn);

    lv_obj_t * saved_ap_label = lv_label_create(saved_ap_btn, NULL);
    lv_obj_add_style(saved_ap_label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
    lv_label_set_long_mode(saved_ap_label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(saved_ap_label, lv_obj_get_width_fit(saved_ap_btn));
    lv_label_set_text_id(saved_ap_label, WATCH_TEXT_ID_SAVED_NW);

#if USE_LV_WATCH_KEYPAD != 0
    lv_obj_t * add_nw_btn = lv_btn_create(list, saved_ap_btn);
    lv_obj_set_event_cb(add_nw_btn, wlan_client_add_nw_btn_event_cb);
    lv_watch_obj_add_element(add_nw_btn);

    lv_obj_t * add_nw_label = lv_label_create(add_nw_btn, saved_ap_label);
    lv_label_set_text_id(add_nw_label, WATCH_TEXT_ID_ADD_NW);
#endif
}

static void wlan_client_test_mode_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    wlan_client_test_mode_create();
}
#endif

static void wlan_update_ap_list_task_cb(lv_task_t * task)
{
    wlan_ext_t * ext = wlan_get_ext(ACT_ID_WLAN);
    if(NULL == ext) return;

    printf("%s\n", __FUNCTION__);

    wlan_add_saved_nw_list(ext);
}

static void wlan_closed_task_cb(lv_task_t * task)
{
    wlan_ext_t * ext = wlan_get_ext(ACT_ID_WLAN);
    if(NULL == ext) {
        printf("%s: no wlan ui\n", __FUNCTION__);
        return;
    }

    wlan_add_saved_nw_list(ext);
}

static void wlan_event_cb(HAL_WLAN_EVENT_T event)
{
    if(lv_watch_get_activity_obj(ACT_ID_WLAN) && (true == watch_get_lcd_status())) {
        lv_task_t * task = NULL;
        if(HAL_WLAN_EVENT_UPDATE_AP_LIST == event) {
            task = lv_task_create(wlan_update_ap_list_task_cb, 10, LV_TASK_PRIO_HIGH, NULL);
        } else if(HAL_WLAN_EVENT_WLAN_CLOSED == event) {
            task = lv_task_create(wlan_closed_task_cb, 10, LV_TASK_PRIO_HIGH, NULL);
        }
        if(task) {
            lv_task_once(task);
        }
    }
}

static void wlan_add_note_label(lv_obj_t ** note_label, lv_obj_t * par, uint16_t txt_id)
{
    printf("%s: txt id %d, note label %p\n", __FUNCTION__, txt_id, note_label);
    lv_obj_t * label = *note_label;
    if(NULL == label) {
        label = lv_label_create(par, NULL);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, WLAN_STYLE_FONT);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
        lv_obj_set_width(label, lv_obj_get_width(par) - 40);
        *note_label = label;
    }
    lv_label_set_text_id(label, txt_id);
    lv_obj_align(label, par, LV_ALIGN_CENTER, 0, 0);
}

static void wlan_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);
    hal_wlan_ui_state_chg(false);
}

static lv_obj_t * wlan_create(lv_obj_t * activity_obj)
{
    hal_wlan_ui_state_chg(true);

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_WLAN;
        activity_ext.create = wlan_create;
        activity_ext.prepare_destory = wlan_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    wlan_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(wlan_ext_t));
    LV_ASSERT_MEM(ext);

    ext->note_label = NULL;

    lv_obj_t * cont = wlan_add_bg(obj);

    lv_obj_t * title_cont = wlan_add_title(cont, WATCH_TEXT_ID_WLAN);

#if WLAN_CLIENT_TEST_MODE != 0
    lv_obj_t * option_btn = lv_btn_create(cont, NULL);
    lv_obj_set_size(option_btn, 50, 40);
    lv_obj_add_style(option_btn, LV_BTN_PART_MAIN, &lv_style_transp_tight);
    lv_obj_set_style_local_bg_color(option_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_opa(option_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_OPA_20);
    lv_obj_set_style_local_radius(option_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 15);
    lv_obj_set_event_cb(option_btn, wlan_client_test_mode_btn_event_cb);
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(cont)) {
        lv_obj_align(option_btn, cont, LV_ALIGN_IN_TOP_LEFT, 4, 4);
    } else {
        lv_obj_align(option_btn, cont, LV_ALIGN_IN_TOP_RIGHT, -4, 4);
    }

    lv_obj_t * option_img = lv_img_create(option_btn, NULL);
    lv_img_set_auto_size(option_img, true);
    lv_img_set_src(option_img, ICON_SETTING_OPTION);
    lv_obj_align(option_img, option_btn, LV_ALIGN_CENTER, 0, 0);
#endif

    ext->list = lv_list_create(cont, NULL);
    wlan_list_set_common_param(ext->list);
    lv_obj_set_size(ext->list,
                    lv_obj_get_width_fit(cont),
                    lv_obj_get_height(cont) - lv_obj_get_y(title_cont)
                    - lv_obj_get_height(title_cont) - 4);
    lv_obj_align(ext->list, cont, LV_ALIGN_IN_BOTTOM_MID, 0, -2);

    wlan_add_saved_nw_list(ext);

    return obj;
}

#endif /* WLAN_MANUAL_SET_WIFI_SUPP */

#endif /*USE_LV_WATCH_WLAN*/
